/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/14
 * Time: 17:27
 */
public class Solution {
    public int longestOnes(int[] nums, int k) {
        int left = 0,right = 0;
        int count = Integer.MIN_VALUE;
        int zero = 0;
        while(right < nums.length){
            if(nums[right] == 0) {
                zero++;
            }
            //出窗口,直到zero <= k
            if(zero > k){
                do {
                    if (nums[left] == 0) {
                        zero--;
                    }
                    left++;
                } while (zero != k);
            }
            right++;
            count = Math.max(count,right - left);
        }
        return count;
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        int ret = s.longestOnes(new int[]{1, 0, 1, 0, 0, 1, 1, 1}, 2);
        System.out.println(ret);
    }
}
